package myjava.algorithm.leetcode;

public class countAndSay {
    public static String countAndSay(int n) {
        if(n==1) return "1";
        
        String start = "1";

        char a;
        int counter;
        
        for(int i=1;i<n;i++) {
            StringBuilder result = new StringBuilder();
            a = start.charAt(0);
            counter =1;
            for(int k=1;k<start.length();k++) {
                if(a == start.charAt(k)) counter++;
                else{
                    result.append(counter);                                        
                    result.append(a);
                    a = start.charAt(k);
                    counter = 1;
                }
            }
            result.append(counter);                                        
            result.append(a);
            
            start = result.toString();
        }
        return start;
    }

    public static void main(String[] args) {
        System.out.println(countAndSay(6));
    }
}
